package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-01-27 13:53
 **/
public class LeetCode2047 {

    public static void main(String[] args) {
        LeetCode2047 code = new LeetCode2047();
        System.out.println(code.countValidWords("cat and  dog"));
        System.out.println(code.countValidWords(" 62   nvtk0wr4f  8 qt3r! w1ph 1l ,e0d 0n 2v 7c.  n06huu2n9 s9   ui4 nsr!d7olr  q-, vqdo!btpmtmui.bb83lf g .!v9-lg 2fyoykex uy5a 8v whvu8 .y sc5 -0n4 zo pfgju 5u 4 3x,3!wl  fv4   s  aig cf j1 a i  8m5o1  !u n!.1tz87d3 .9    n a3  .xb1p9f  b1i a j8s2 cugf l494cx1! hisceovf3 8d93 sg 4r.f1z9w   4- cb r97jo hln3s h2 o .  8dx08as7l!mcmc isa49afk i1 fk,s e !1 ln rt2vhu 4ks4zq c w  o- 6  5!.n8ten0 6mk 2k2y3e335,yj  h p3 5 -0  5g1c  tr49, ,qp9 -v p  7p4v110926wwr h x wklq u zo 16. !8  u63n0c l3 yckifu 1cgz t.i   lh w xa l,jt   hpi ng-gvtk8 9 j u9qfcd!2  kyu42v dmv.cst6i5fo rxhw4wvp2 1 okc8!  z aribcam0  cp-zp,!e x  agj-gb3 !om3934 k vnuo056h g7 t-6j! 8w8fncebuj-lq    inzqhw v39,  f e 9. 50 , ru3r  mbuab  6  wz dw79.av2xp . gbmy gc s6pi pra4fo9fwq k   j-ppy -3vpf   o k4hy3 -!..5s ,2 k5 j p38dtd   !i   b!fgj,nx qgif "));
    }

    public int countValidWords(String sentence) {
        String[] split = sentence.split("\\s+");
        //有效单词的数目
        int count = 0;
        for (String s : split) {
            int n = s.length();
            //cnt: '-'字符的个数
            int cnt = 0;
            //是否存在'-'字符
            boolean f1 = false;
            //'-'字符右边是否存在字母
            boolean f2 = false;
            for (int i = 0; i < n; i++) {
                char c = s.charAt(i);
                if ((0 <= c - '0' && c - '0' <= 9) || ((c == '!' || c == '.' || c == ',') && i != n - 1)) {
                    break;
                }

                if (c == '-') {
                    ++cnt;
                    if (i == 0 || i == n - 1 || cnt > 1) {
                        break;
                    }
                    //中间存在'-'字符,置f1为true
                    f1 = true;
                } else {
                    //如果存在'-'字符且右边遇到了字母,就更新f2为true
                    if (f1 && Character.isLetter(c)) {
                        f2 = true;
                    }
                }

                if (i == n - 1) {
                    //如果不存在'-'(即f1为false)或者存在'-'且两边都存在字母(即f1和f2都为true)
                    if (!f1 || (f1 && f2)) {
                        ++count;
                    }
                }
            }
        }
        return count;
    }

}
